package com.godyao.mall.pms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.godyao.mall.pms.dao.BrandDao;
import com.godyao.mall.pms.dao.CategoryDao;
import com.godyao.mall.pms.entity.BrandEntity;
import com.godyao.mall.pms.entity.CategoryEntity;
import com.godyao.mall.pms.service.BrandService;
import com.godyao.mall.pms.service.CategoryService;
import com.godyao.mall.pms.vo.response.BrandResponseVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.godyao.mall.core.utils.PageUtils;
import com.godyao.mall.core.utils.Query;

import com.godyao.mall.pms.dao.CategoryBrandRelationDao;
import com.godyao.mall.pms.entity.CategoryBrandRelationEntity;
import com.godyao.mall.pms.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
@RequiredArgsConstructor
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    private final BrandDao brandDao;

    private final CategoryDao categoryDao;


    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        final Long brandId = categoryBrandRelation.getBrandId();
        final Long catelogId = categoryBrandRelation.getCatelogId();
        final BrandEntity brandEntity = Optional.ofNullable(brandDao.selectById(brandId)).orElse(new BrandEntity());
        final CategoryEntity categoryEntity = Optional.ofNullable(categoryDao.selectById(catelogId)).orElse(new CategoryEntity());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandResponseVo> brandList(Long catId) {
        return this.lambdaQuery()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId)
                .list()
                .stream()
                .map(categoryBrandRelationEntity -> {
                    final BrandResponseVo brandResponseVo = new BrandResponseVo();
                    brandResponseVo.setBrandId(categoryBrandRelationEntity.getBrandId());
                    brandResponseVo.setBrandName(categoryBrandRelationEntity.getBrandName());
                    return brandResponseVo;
                })
                .collect(Collectors.toList());

    }

    @Override
    public PageUtils queryPage(Long brandId, Map<String, Object> params) {
        final IPage<CategoryBrandRelationEntity> page = this.lambdaQuery()
                .eq(ObjectUtil.isNotNull(brandId), CategoryBrandRelationEntity::getBrandId, brandId)
                .page(new Query<CategoryBrandRelationEntity>().getPage(params));
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

}
